from django.shortcuts import render,redirect,loader
from .models import *
from Organization.models import *
from django.db.models import *
from hashlib import md5
from django.http import HttpResponse

# Create your views here.
'''
    def:login:实现输入网址即可进入登陆界面login.html
'''
def login(requst):
    return render(requst,'user/login.html')
'''
    def login_handler:
    username:获取网页中'用户名'
    password:获取网页中'密码'
    isChecked:获取网页中多选框是否选中后的值
'''
def login_handler(request):
    username =request.POST.get('')
    password = request.POST.get('')
    isChecked = request.POST.get('')
    u=User()
    if password != '':
        if u.findLogin(username,password):
            request.session['user'] = username
            if isChecked == 'on':
                content = loader.render_to_string('User/login.html',request)
                hp = HttpResponse(content)
                hp.set_cookie('user',username)
                return hp
            return render(request,'User/login.html')
        return render(request,'User/login.html')
    return render(request,'User/login.html')

'''
    def register：实现输入网址即可进入注册界面register.html
'''

def register(request):
    return render(request,'user/register.html')
'''
    def register_handler:
    username:获取网页中'用户名'C_username的值
    password1:获取网页中'密码'C_password1的值
    password2:获取网页中'确认密码'C_password2的值
    email:获取网页中'邮件'C_email的值
    staffs:获取网页中'员工编号'C_staffs的值
    roles:获取网页中'权限编号'C_roles的值
    u:实例化User()后的对象，拥有User()中create与findregister的方法
'''
def regiter_handler(request):
    username = request.POST.get('C_username')
    password1 = request.POST.get('C_password1')
    password2 = request.POST.get('C_password2')
    email = request.POST.get('C_email')
    staffs = request.POST.get('C_staffs')
    roles = request.POST.get('C_roles')
    u=User()
    if u.findRegister(username):
        return redirect('/User/register/')
    else:
        if password1 == password2:
            u.createUser()
            return render(request, 'User/login.html')
        else:
            return render(request,'User/register.html')
